package com.example.gs.mvpdemo.presenter.imp;

import com.example.gs.mvpdemo.ProApplication;
import com.example.gs.mvpdemo.view.activity.LoginActivity;
import com.example.gs.mvpdemo.base.BasePresenter;
import com.example.gs.mvpdemo.model.bean.LoginBean;
import com.example.gs.mvpdemo.contract.LoginContract;
import com.example.gs.mvpdemo.exception.ApiException;
import com.example.gs.mvpdemo.model.imp.LoginModelImp;
import com.example.gs.mvpdemo.model.IModel;
import com.example.gs.mvpdemo.subscriber.CommonSubscriber;
import com.example.gs.mvpdemo.utils.LogUtils;

import java.util.HashMap;

/**
 * Created by GaoSheng on 2016/11/26.
 * 20:51
 *
 * @VERSION V1.4
 * com.example.gs.mvpdemo.presenter
 */

public class LoginPresenterImp extends BasePresenter<LoginActivity> implements
        LoginContract.LoginPresenter {
    /**
     * 登录方法---------根据LoginModel返回的数据进行处理
     *
     * @param name
     * @param pwd
     */
    @Override
    public void login(String name, String pwd) {
        if (!getIView().checkNull()) {
            ((LoginModelImp) getiModelMap().get("login"))
                    .login(name, pwd, new CommonSubscriber<LoginBean>(ProApplication.getmContext()) {
                        @Override
                        public void onNext(LoginBean loginBean) {
                            getIView().loginSuccess(loginBean.getToken());//成功

                        }

                        @Override
                        protected void onError(ApiException e) {
                            super.onError(e);
                            LogUtils.e("LoginPresenter.failInfo", e.message);

                            getIView().loginFail(e.message);  //失败
                        }
                    });
        }
    }


    @Override
    public HashMap<String, IModel> getiModelMap() {
        return loadModelMap(new LoginModelImp());
    }

    @Override
    public HashMap<String, IModel> loadModelMap(IModel... models) {
        HashMap<String, IModel> map = new HashMap<>();
        map.put("login", models[0]);
        return map;
    }
}
